<template>
	<view class='content'>
        <!-- #ifdef MP-WEIXIN --> 
		<top-nav :inner-title="title" is-show-back="false" is-fixed="true" :navH="navH" :n="n" is-white="bg-white" opacity="1"></top-nav>
		<view :style="{height:navH + 'px'}"></view>
        <!-- #endif -->
        <view style="height:10px"></view>
		<block v-if='orderList.length > 0'>
		  <block v-for='(item,index) in orderList' :key="index">
			<view class='item'>
				<view class="item_row">
					<view class="icon">
						<image :src='item.store_icon'  @click='goStore(item.store_id, item.service_type)'></image>
					</view>
					<view class="item_right">
						<view class='title' @click='goStore(item.store_id, item.service_type)'>
							<view class='text'>
								<view class='name'>
									<view class="name_inner ellipsis"> {{item.store_name}}</view>
									<icon class="arr_right"></icon>
								</view>
								<view class='status'>
									<block v-if="item.status == 1">
										<text>等待商家接单</text>
									</block>
									<!-- 配送 -->
									<block v-if="item.service_type == 0">
										<block v-if="item.status == 4">
											<text>商家已接单</text>
										</block>
										<block v-if="item.status == 11 || item.status == 5 || item.status == 9">
											<text>商品已送出</text>
										</block>
									</block>
									<!-- 自取 -->
									<block v-else>
										<block v-if="item.status == 5">
											<text>商家已接单</text>
										</block>
									</block>
									<block v-if="item.status == 7">
										<text>订单已完成</text>
									</block>
									<block v-if="item.status == 8">
										<text>商家接单超时</text>
									</block>
									<block v-if="item.status == 6">
										<text>订单已取消</text>
									</block>
									<block v-if="item.status == 10">
										<text>订单异常中</text>
									</block>
								<!---团购状态-->
									<block v-if="item.status == 21">
										<text>等待开车</text>
									</block>
									<block v-if="item.status == 22">
										<text>开车成功</text>
									</block>
									<block v-if="item.status == 23">
										<text>开车失败</text>
									</block>
									<block v-if="item.status == 24">
										<text>开车成功</text>
									</block>
									<block v-if="item.status == 25">
										<text>商品已送出</text>
									</block>
									<block v-if="item.status == 26">
										<text>订单已完成</text>
									</block>
									<block v-if="item.status == 27">
										<text>订单已取消</text>
									</block>
								<!---团购状态-->
								</view>
							</view>
						</view>
						<view class="order_time" @click='goDetail(item.order_no)'>
							{{item.pay_time}}
							<text class="order_num_green" v-if="item.order_num>0">
								<text>取餐号 #</text>
								<text class="order_num">{{item.order_num}}</text>
							</text>
						</view>
						<view class='list' @click='goDetail(item.order_no)'>
							<view class='box'></view>
							<view class='list-info'>
								<block>
									<view class='list-item'>
										<text>{{item.goods_list[0].name}} 等{{item.goods_count}}件商品</text>
										<text>￥{{item.real_price}}</text>
									</view>
								</block>
							</view>
						</view>
					</view>
				</view>
				<view  class="item_row center" v-if="item.order_num>0 || item.group_no <= 0">
					<view>
						<view class="left-red" v-if="item.order_num>0" @click='goDetail(item.order_no)'>
							<!-- <text>取餐号</text>
							<text class="order_num">{{item.order_num}}</text> -->
						</view>
					</view>
					<view class='list-detail flex'>
						<view v-if="item.group_no <= 0">
							<view class="orderMore"  @click='goStore(item.store_id, item.service_type)'></view>
						</view>
                        <!-- <view>
                        	<view class="orderMore contact_deli"></view>
                        </view>
                        <view>
                        	<view class="orderMore contact_shop"></view>
                        </view> -->
                        <view>
                        	<view class="orderMore judge"  @click='goComment(item.order_no,item.store_id,item.service_type)' v-if="item.is_comment == 1 && item.is_over == 0 && (item.status == 26 || item.status == 7)"></view>
                        </view>
					</view>
				</view>
				<view class="item_border" v-if="index < orderList.length-1"></view>
			</view>
		  </block>
			<view class="no-more" v-if="noMore">没有更多了哦</view>
		</block>
		<block v-else>
		  <view class='empty' @click="loadData()">
			  <view class='img'></view>
			  <text>您还没有订单哦</text>
		  </view>
		</block>
	</view>
</template>

<script>
	import uniFly from 'unifly';
	import request from '../../common/request.js';
	import $api_base from '../../api/api_base.js';
	import topNav from '@/template/topnav/index.vue';
    import event from '../../common/event.js';
	export default {
		components: { topNav },
		data() {
			return {
				orderList: [],
				add_time: '',
				hasMore: 0,
				isEmpty: true,
				navH: 0,
				n:0,
				title: '订单',
				noMore:false,
                rp_id:0
			}
		},
		onLoad: function (options) {
            var self = this;
			var orderNo = options.order_no || '';
			var sid = options.sid || '';
			var from = options.from || '';
            this.rp_id = options.rp_id || ''
			if (sid == 'pay_success' && orderNo != undefined) {
				uni.navigateTo({
					url: '/pagesGoods/order-info/index?order_no=' + orderNo + '&from=' + from + '&rp_id=' + self.rp_id,
				});
			}
			this.initApp();
			event.on('ReloadOrderList', this, function () {
				this.loadData(false, true);
			});
			this.navH = getApp().globalData.navHeight;
			this.n = getApp().globalData.n;
		},
		 onShow: function () {
			this.loadData(false, false);
		},
		methods:{
			goDetail: function (id) {
                var self = this;
				var orderNo = id;
				uni.navigateTo({
					url: '/pagesGoods/order-info/index?order_no=' + orderNo,
				})
			},
			goStore: function (id,type) {
				var storeId = id;
				var servicetype = type;
				uni.navigateTo({
					url: '/pages/goods/goods?storeId=' + storeId + '&sid=order_list' + '&servicetype=' + servicetype,
				})
			},
			initApp: function () {
				var self = this;
				let params = {
					long_lat: getApp().longLat
				};
				self.loginReg().then(function () {
					//定位
					$api_base.post($api_base.homeIndex,params).then(function(res) {
						let data = res.data;
						if (data.ok == 1 || data.ok == 2) {     //汤为说全局搜索homeIndx
							getApp().globalData.schoolInfo = data.schoolInfo;
							getApp().globalData.userInfo = data.userInfo;
							getApp().globalData.schoolList = data.schoolList;
							getApp().kfPhone = data.kfPhone;
							getApp().isLogin = true;
							self.loadData(false, true);
							uni.setStorageSync('uid', data.userInfo.uid);
						}
					});
				});
			},
			loadData: function (isAppend, mask) {
				if (!getApp().isLogin) return false;
				var where = new Object();
				var self = this;
				where.add_time = this.add_time;
				$api_base.post($api_base.getOrderList,where,1).then(function(res) {
					let data = res.data;
					if (data.ok == 1) {
						var isEmpty = data.list.length > 0 ? true : false;
						self.isEmpty = isEmpty;
						self.hasMore = data.has_more;
						if (isAppend) {
							self.orderList = [...self.orderList, ...data.list];
						} else {
							self.orderList = data.list;
						};
					}
					if (data.has_more > 0) {
						self.add_time = data.add_time;
					}
				});
			},
            goComment: function(id,m,s){
                var orderNo = id;
                var storeId = m;
                var servicetype = s;
                uni.navigateTo({
                    url:'/pagesOther/comment/index?order_no=' + orderNo + '&storeId=' + storeId + '&serviceType=' + servicetype ,
                })
            },
			/**
			 * 页面相关事件处理函数--监听用户下拉动作
			 */
			onPullDownRefresh: function () {
				uni.stopPullDownRefresh();
			},
			/**
			 * 页面上拉触底事件的处理函数
			 */
			onReachBottom: function () {
			  this.noMore = true;
			},
		}
	}
</script>

<style>
  page {
  background-color: #fff;
  }
</style>
<style lang="scss" scoped>
	@import 'index.scss';
</style>
